﻿/*
 * Created by SharpDevelop.
 * User: Intel
 * Date: 18.11.2014
 * Time: 13:15
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Region.Utils;

namespace Region
{
  /// <summary>
  /// Description of UserSelectorForm.
  /// </summary>
  public partial class UserSelectorForm : Form
  {
    protected UserList m_userListNull;
    protected Engine m_engine;
    protected UserList m_userList; 
    protected UserList m_currentUserList; 
    protected Int32 m_UserID;
    public Engine engine{get{return m_engine;}set{m_engine=value;change();}}
    public UserList userList{get{return m_userList;}set{m_userList=value;change();}}
    public Int32 UserID{get{return m_UserID;}set{m_UserID=value;change();}}
    public delegate void EventHandlerOnSelectUser(Int32 UserID);    
    public event EventHandlerOnSelectUser OnSelectUser;
    Dictionary<Int32,ListItemValue> listItems;
    Dictionary<Int32,TreeNode> listNodes;
    protected void changeUserID(Int32 UserID)
    {
      if (m_UserID == UserID) return;
      m_UserID = UserID;
      if (listItems!=null && listItems.ContainsKey(m_UserID) && listBox1.SelectedItem != listItems[m_UserID])        
        listBox1.SelectedItem = listItems[m_UserID];
      if (listNodes!=null && listNodes.ContainsKey(m_UserID) && treeView1.SelectedNode != listNodes[m_UserID])
        treeView1.SelectedNode = listNodes[m_UserID];
      if (OnSelectUser!=null) 
        OnSelectUser(m_UserID);
    }
    protected void change()
    {
      if (m_engine==null) return;
    }
    public UserSelectorForm()
    {
      //
      // The InitializeComponent() call is required for Windows Forms designer support.
      //
      InitializeComponent();
      //
      // TODO: Add constructor code after the InitializeComponent() call.
      //
    }
    public void ShowList()
    {
      if (m_engine==null) return;
      m_currentUserList = m_userList;
      if (m_currentUserList==null)
      {
        if (m_userListNull==null) m_userListNull = new UserList(m_engine);
        m_currentUserList = m_userListNull;
      }
      m_currentUserList.Filter();
      treeView1.Nodes.Clear();
      listNodes = new Dictionary<Int32, TreeNode>();
      var Otdels = new Dictionary<Int32, Boolean>();
      var OtdelsNode = new Dictionary<Int32, TreeNode>();
      for(int i=0;i<m_currentUserList.NumLines;i++)
      {
        UserListLine line = m_currentUserList.getLine(i);
        if (Otdels.ContainsKey(line.OtdelID)) continue;
        Otdel otdel = m_engine.getOtdel(line.OtdelID);
        while(otdel!=null && !Otdels.ContainsKey(otdel.OtdelID))
        {
          Int32 OtdelID = otdel.OtdelID;
          otdel = m_engine.getOtdel(otdel.ParentOtdelID);
          Otdels[OtdelID] = otdel==null;
        }        
      }
      Boolean workOtdel = true;
      while(workOtdel)
      {
        workOtdel = false;
        foreach (var o in Otdels)
          if (!OtdelsNode.ContainsKey(o.Key))
        {
          Otdel otdel = m_engine.getOtdel(o.Key);
          if (OtdelsNode.ContainsKey(otdel.ParentOtdelID))
          {
            var node = new TreeNode(otdel.OtdelName);
            OtdelsNode[otdel.OtdelID] = node;
            OtdelsNode[otdel.ParentOtdelID].Nodes.Add(node);
            workOtdel = true;
          }
          else if (o.Value)
          {
            var node = new TreeNode(otdel.OtdelName);
            OtdelsNode[otdel.OtdelID] = node;
            treeView1.Nodes.Add(node);
            workOtdel = true;
          }
        }
      }
      listBox1.Items.Clear(); 
      listItems = new Dictionary<Int32, ListItemValue>();
      for(int i=0;i<m_currentUserList.NumLines;i++)
      {
        
        UserListLine line = m_currentUserList.getLine(i);
        var user_liv = new ListItemValue(line.FIO,line.UserID);
        listItems[line.UserID]=user_liv;
        listBox1.Items.Add(user_liv);
        if (line.UserID == m_UserID) 
          listBox1.SelectedItem = user_liv;
        if (OtdelsNode.ContainsKey(line.OtdelID))
        {
          var nodeUser = new TreeNode(line.FIO);
          nodeUser.Tag = line.UserID;
          OtdelsNode[line.OtdelID].Nodes.Add(nodeUser);
          listNodes[line.UserID] = nodeUser;
          if (line.UserID == m_UserID) 
            treeView1.SelectedNode = nodeUser;
        }
      }
      listBox1.SelectedItem = listItems.ContainsKey(m_UserID)?listItems[m_UserID]:null;
      treeView1.SelectedNode = listNodes.ContainsKey(m_UserID)?listNodes[m_UserID]:null;
      Show();
    }
		void TabPage2Click(object sender, EventArgs e)
		{

		}
		void UserSelectorFormDeactivate(object sender, EventArgs e)
		{
		  Hide();
		}
		void FindLineTextChanged(object sender, EventArgs e)
		{
		  if (m_currentUserList==null) return;
      listBox1.Items.Clear();
      for(int i=0;i<m_currentUserList.NumLines;i++)
      {
        UserListLine line = m_currentUserList.getLine(i);
        if (line.FIO.ToUpper().IndexOf(findLine.Text.ToUpper())>=0||line.UserID==m_UserID)
          listBox1.Items.Add(new ListItemValue(line.FIO,line.UserID));
      }	   
		}
		void TreeView1BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
		  if (e.Node!=null && e.Node.Tag is Int32)
		  {
		    changeUserID((Int32)e.Node.Tag);
		  }
		}
		void ListBox1SelectedIndexChanged(object sender, EventArgs e)
		{
		  if (listBox1.SelectedItem==null) return;
		  var liv = listBox1.SelectedItem as ListItemValue;
		  if (liv==null) return;
		  changeUserID((Int32)liv.Value);
		}
  }
}
